package com.navcoo.todolist.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.navcoo.todolist.mapper.TodoDataMapper;
import com.navcoo.todolist.pojo.dao.TodoDataDAO;
import com.navcoo.todolist.service.SearchTodoDatas;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 搜索待办数据
 *
 * @author Navcoo
 * @date 2024-02-12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SearchTodoDatasImpl implements SearchTodoDatas {

    /**
     * Todo数据映射器
     */
    private final TodoDataMapper todoDataMapper;


    /**
     * 搜索待办事项数据
     *
     * @param pageSize    页面大小
     * @param currentPage 当前页面
     * @param isDone      是否完成
     * @return {@link Page}<{@link TodoDataDAO}>
     * @throws Exception 异常
     */
    @Override
    public Page<TodoDataDAO> searchTodoDatas(Integer pageSize, Integer currentPage, Boolean isDone) throws Exception {

        // 构建查询条件
        QueryWrapper<TodoDataDAO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TodoDataDAO::getIsDelete, false);
        // 查询条件
        if (BooleanUtil.isTrue(isDone) || BooleanUtil.isFalse(isDone))
            wrapper.lambda().eq(TodoDataDAO::getIsDone, isDone);


        // 查询数据分页
        Page<TodoDataDAO> todoDataDAOPage = new Page<>(currentPage, pageSize);
        // 查询数据
        todoDataMapper.selectPage(todoDataDAOPage, wrapper);

        log.info("查询到数据条数：{}", todoDataDAOPage.getTotal());
        log.info("查询到数据：{}", todoDataDAOPage.getRecords());
        return todoDataDAOPage;
    }

}
